%%% -------------------------------------------------------------------
%%% Author  : Administrator
%%% Description :
%%%
%%% Created : 2010-2-6
%%% -------------------------------------------------------------------
-module(mgee_chat_sup).

-behaviour(supervisor).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include("mgee.hrl").

%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([
		 start/0,
		 get_chat_name/1,
		 start_link/0
		 ]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([
	 init/1
        ]).

%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------
-define(SERVER, ?MODULE).

%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------

%% ====================================================================
%% External functions
%% ====================================================================

start() ->
	{ok, _} = supervisor:start_child(mgee_sup, {mgee_chat_sup,
                {mgee_chat_sup, start_link,[]},
                permanent, infinity, supervisor, [mgee_chat_sup]}).

start_link() ->
	R = supervisor:start_link({local, ?SERVER}, ?MODULE, []),
	lists:foreach(
		fun(T) ->
			supervisor:start_child(?SERVER, [get_chat_name(T)])
		end, lists:seq(1, erlang:system_info(schedulers))),
	R.

%% ====================================================================
%% Server functions
%% ====================================================================
%% --------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok,  {SupFlags,  [ChildSpec]}} |
%%          ignore                          |
%%          {error, Reason}
%% --------------------------------------------------------------------
init([]) ->
	?INFO_MSG("~p init: ~p",[?MODULE, [] ]),
    AChild = {mgee_chat,{mgee_chat, start_link, []},
	      permanent, brutal_kill, worker, [mgee_chat]},
    {ok,{{simple_one_for_one,10,10}, [AChild]}}.

%% ====================================================================
%% Internal functions
%% ====================================================================

get_chat_name(N) ->
	mgee_tool:list_to_atom2(lists:concat([mgee_chat_, N])).